package access

import (
	"gitlab.local/TerraMaster/tos-modules/timesetting"
	"os"
	"strings"

	"gitlab.local/DO-module/new-filemanage-module/src/core"
	"gitlab.local/DO-module/new-filemanage-module/src/core/sqlitedb"
	"gitlab.local/DO-module/new-filemanage-module/src/service/access/constant"
	"gitlab.local/golibrary/language"
	"gitlab.local/golibrary/sysconfig"
	"gitlab.local/golibrary/utils"
)

var DefaultServe = []AccessDatas{
	{AppId: "TOS", AppName: "TOS", IsDefault: true},
	//{AppId: "AFP", AppName: "AFP", IsDefault: true},
	{AppId: "FTP", AppName: "FTP", IsDefault: true},
	{AppId: "SFTP", AppName: "SFTP", IsDefault: true},
	{AppId: "SMB", AppName: "SMB", IsDefault: true},
	{AppId: "filemanage", AppName: "File Manager", IsDefault: true},
}

type AccessDatas struct {
	AppId     string `json:"app_id"` // app name without blank
	AppName   string `json:"name"`   // app name with blank
	IsDefault bool   `json:"default"`
}

type UserAccess struct {
	Username string `json:"username"`
	IsAdmin  bool   `json:"is_admin"` // 是否是管理员 管理员禁止操作
	IsCheck  bool   `json:"is_check"` // user: true:权限勾选上;false:不勾选，根据用户组分配权限    group: true:勾选;false:不勾选,权限为允许
	IsAllow  int    `json:"is_allow"`
}

type AppsList struct {
	AppId            string   `json:"id"`       // app名称1
	AppName          string   `json:"name"`     // app名称2，2207130955 目前只观察到只有一个应用名称1和名称2不一样 id: "filemanage" name: "File Manager"
	IsDefault        bool     `json:"default"`  // 是否是系统默认应用，见 DefaultServe
	AllowUserList    []string `json:"allow"`    // 允许 访问应用的用户
	DisallowUserList []string `json:"disallow"` // 禁止 访问应用的用户
}

type AppInfoAccess struct {
	User  []UserAccess `json:"user"`
	Group []UserAccess `json:"group"`
}

type EditAccessData struct {
	AppId string        `json:"app_id"`
	List  AppInfoAccess `json:"list"`
}

type Access struct {
	db            sqlitedb.Getter
	coreGetter    core.ConfigGetter
	langManager   language.Manager
	timeGetter    timesetting.Getter
	configService sysconfig.GetSetter
}

type UserSlice []UserAccess

func (us UserSlice) Len() int {
	return len(us)
}
func (us UserSlice) Swap(i, j int) {
	us[i], us[j] = us[j], us[i]
}
func (us UserSlice) Less(i, j int) bool {
	return strings.Compare(us[i].Username, us[j].Username) < 0
}

/*func New(
	db sqlitedb.Getter,
	configService sysconfig.GetSetter,
	langManager language.Manager,
) *Access {
	return &Access{
		db:            db,
		langManager:   langManager,
		configService: configService,
		coreGetter:    core.New(),
	}
}*/

func MkdirPath() {
	if !utils.Exists(constant.PamConf) {
		_ = os.Mkdir(constant.PamConf, 0o777)
	}
}
